package com.pan;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

/**
 * -WordToHtml
 * <p>
 * By:panpan.wang 2019年11月28日
 */
public class WordToHtml {
    public static void pptToHtml(String pptPath, String htmlPath) {
        ActiveXComponent app = new ActiveXComponent("Word.Application");// 启动word
        try {
            app.setProperty("Visible", new Variant(false));
            // 设置word不可见
            Dispatch docs = app.getProperty("Documents").toDispatch();
            Dispatch doc = Dispatch.invoke(
                    docs,
                    "Open",
                    Dispatch.Method,
                    new Object[]{pptPath, new Variant(false),
                            new Variant(true)}, new int[1]).toDispatch();
            // 打开word文件
            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{
                    htmlPath, new Variant(8)}, new int[1]);
            // 作为html格式保存到临时文件
            Variant f = new Variant(false);
            Dispatch.call(doc, "Close", f);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            app.invoke("Quit", new Variant[]{});
        }
        System.out.println("转化完毕！");
    }

    public static void main(String[] args) {
        String paths = new String("I:\\test.docx");
        String savepaths = new String("I:\\aa.html");

        pptToHtml(paths, savepaths);
    }
}
